<template>
  <div>
    <!-- 上部分 -->
    <el-card class="box-card" style="margin: 20px 0px">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene == 0" />
    </el-card>
    <!-- 下部分 -->
    <el-card>
      <!-- 底部这里有三个部分切换 -->
      <!-- 第一部分 SPU列表的展示 -->
      <div v-show="scene == 0">
        <!-- 展示SPU列表的结构 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!cForm.category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="spuName"
            label="spu名称"
            width="width"
          ></el-table-column>
          <el-table-column
            prop="description"
            label="spu描述"
            width="width"
          ></el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <hin-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hin-button>
              <hin-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></hin-button>
              <hin-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="handler(row)" 
              ></hin-button>
              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <hin-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></hin-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- @size-change="handleSizeChange"
        @current-change="handleCurrentChange" -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev,pager,next, jumper,->, sizes,total"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>

      <!-- 第二部分 -->
      <spu-form
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="spu"
      ></spu-form>
      <!-- 第三部分 -->
      <sku-form v-show="scene == 2" ref="sku" @changeScene="changeScene"></sku-form>
    </el-card>
    <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
      <!-- table展示sku列表-->
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{row,$index}">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px;">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import CategorySelect from "@/components/CategorySelect";
import spuForm from "./SpuForm";
import skuForm from "./SkuForm";
export default {
  name: "Spu",
  components: {
    CategorySelect,
    spuForm,
    skuForm,
  },
  data() {
    return {
      cForm: {},//收集分类ID
      page: 1, //
      limit: 3,
      records: [],
      total: 0,
      scene: 0, //0 spu列表的展示     1 添加spu或修改spu     2 添加spu
      visible: false,
      dialogTableVisible: false,//控制对话框的显示与隐藏
      spu: {},
      skuList: [], //存储的是SKU列表的数据
      loading: true,
    };
  },
  methods: {
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //三级联动自定义事件，把子组件相应的ID传给父组件
    getCategoryId(cForm) {
      console.log(cForm);
      this.cForm = cForm;
      this.getSpuList();
    },
    //获取SPU列表
    getSpuList() {
      const { page, limit, cForm } = this;
      let result = this.$API.spu.reqSpuList(page, limit, cForm.category3Id); //返回一个promise对象
      result.then((v) => {
        if (v.code == 200) {
          this.total = v.data.total;
          this.records = v.data.records;
        }
      });
    },
    //添加SPU按钮回调
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.cForm.category3Id);
    },
    //修改某个SPU回调
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    //自定义事件回调（SpuForm组件）
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "添加") {
        console.log(22);
        this.page = 1;
      }
      this.getSpuList();
    },
    //删除SPU回调
    async deleteSpu(row) {
      console.log(row);
      console.log(row.id);
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      console.log(row.id);
      if(result.code==200){
        this.$message({type:"success",message:"删除成功"})
        let page=this.records.length>1?this.page:this.page-1
        this.page=page
        this.getSpuList()
      }
      
    },
    //添加SKu回调
    addSku(row){
      this.scene=2
      this.$refs.sku.getData(this.cForm,row)
    },
    //查看SKU的按钮的回调
    async handler(spu) {
      //点击这个按钮的时候，对话框可见的
      this.dialogTableVisible = true;
      //保存spu信息
      this.spu = spu;
      //获取sku列表的数据进行展示
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        //loading隐藏
        this.loading = false;
      }
    },
    //关闭对话框的回调
    close(done) {
      //loading属性再次变为真
      this.loading = true;
      //清除sku列表的数据
      this.skuList = [];
      //管理对话框
      done();
    },
  },
};
</script>

<style>
</style>